#include "GameSelPlane.h"
#include "Game/Game.h"
#include "Game/dConfig.h"
#include "Player/OnceFac.h"
#include "Player/LoopFac.h"
#include "GameRes/AnimMgr.h"
#include "Obj/ObjActor.h"
#include "Obj/Wingman.h"
#include "Obj/ParticleMgr.h"
#include "Obj/Transmitter.h"
#include "Game/Camera.h"
#include "GameRes/DataRecord.h"
#include "Game/SoundPlayer.h"
#include "Game/SoundDef.h"
#include "SimpleAudioEngine.h"
#include <memory>
namespace LTGame
{
	GameSelPlane::GameSelPlane(GST_FLAG flag)
		:GameState(flag)
	{
		planeIndex = 0;
		wingmanIndex = 0;
		noticeBuyPlane = false;
		CCGame->hero_animationID = ANI_HERO1_ID;
		CCGame->wingman_ActionID = 0;
		m_UpSelPlayer = NULL;
		m_DowmSelPlayer = NULL;
		plane_player = NULL;
		glClearColor(0x0, 0x0, 0x0, 0x0);
		AnimMgr::getInstance()->LoadByID(ANI_CG_ID);
		AnimMgr::getInstance()->LoadByID(ANI_BULLET_ID);
		AnimMgr::getInstance()->LoadByID(ANI_LIAOJI_ID);
		AnimMgr::getInstance()->LoadByID(ANI_HERO1_ID);
		AnimMgr::getInstance()->LoadByID(ANI_HERO2_ID);
		AnimMgr::getInstance()->LoadByID(ANI_HERO3_ID);
		AnimMgr::getInstance()->LoadByID(ANI_EFFECTS);
		auto_ptr<AnimPlayerFac> fac(new OnceFac());
		m_UpSelPlayer = fac->CreatePlayer(ANI_CG_ID,0);
		m_DowmSelPlayer = fac->CreatePlayer(ANI_CG_ID,1);
		CCGame->m_uiData->openFrame(Form_SelPlane);
		CCGame->m_uiData->openFrame(Form_SelWm);
		CCGame->m_objMgr->curHero->Hero_CG_Init();
		auto_ptr<AnimPlayerFac> facc(new LoopFac());
		plane_player = facc->CreatePlayer(CCGame->m_objMgr->curHero->m_dat[COBJ_DAT_ANIID],CCGame->m_objMgr->curHero->m_dat[COBJ_DAT_ACTION]);
		Wingman::getInstance()->Wingman_Creat_Single(CCGame->m_objMgr->curHero);
		Wingman::getInstance()->Wingman_Update_Pos();
		Camera::getInstance()->setSpeedY(0);
		if (CCGame->isOpenMusic)
			SoundPlayer::getInstance()->playEffwuqi1(SND_ZIDAN1);
	}
	GameSelPlane::~GameSelPlane()
	{
		AnimMgr::getInstance()->freeRes();
		CC_SAFE_DELETE(m_UpSelPlayer);
		CC_SAFE_DELETE(m_DowmSelPlayer);
		CC_SAFE_DELETE(plane_player);
		CCGame->m_uiData->closeFrame(Form_SelPlane);
		CCGame->m_uiData->closeFrame(Form_SelWm);
		CC_SAFE_DELETE(CCGame->m_objMgr->curHero);
		CParticleList::getInstance()->clearList(PARTICLE_OWNER_ALL);
	}
	bool GameSelPlane::logic(void)
	{
		CParticleList::getInstance()->Logic();
		Wingman::getInstance()->logic();
		CTransmitter::getInstance()->logic();
		return true;
	}
	void GameSelPlane::paint(void)
	{
		m_UpSelPlayer->drawAction(S_WIDTH_HALF,0, false);
		m_DowmSelPlayer->drawAction(S_WIDTH_HALF,S_HEIGHT, false);
		CCGame->m_uiData->drawFrame(Form_SelPlane);
		CCGame->m_uiData->drawFrame(Form_SelWm);
		CParticleList::getInstance()->Paint();
		Wingman::getInstance()->paint();
		plane_player->drawAction(S_WIDTH_HALF/2,S_HEIGHT_HALF, false);
	}
	bool GameSelPlane::game_DoSelPlane_KeyLogic(void)
	{
		int prePlaneIndex = planeIndex;
		int preWingmanIndex = wingmanIndex;

		if ( preWingmanIndex != wingmanIndex)
		{
			Wingman::getInstance()->Wingman_Set_ActionID(wingmanIndex);
			CParticleList::getInstance()->clearList(PARTICLE_OWNER_ALL);
		}
		if ( prePlaneIndex != planeIndex )
		{
			CCGame->m_objMgr->curHero->Hero_CG_upDate(planeIndex);
		}
		return true;
	}
	void GameSelPlane::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
	{
		CCPoint point = pTouch->locationInView( pTouch->view() );
		for (int i = B_Form_Block_2;i <=B_Form_Block_7; i++ )
		{
			if (CCGame->m_uiData->isPointerInBlock(point.x,point.y,Form_SelWm,i))
			{	
				if (wingmanIndex != i - B_Form_Block_2)
				{
					wingmanIndex = i - B_Form_Block_2;
					if (CCGame->isOpenMusic)
						SOUND_PLAY_EFF(SND_OK);
					Wingman::getInstance()->Wingman_Set_ActionID(wingmanIndex);
					switch(wingmanIndex)
					{
						case 0:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,true);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,false);
							break;
						case 1:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,true);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,false);
							break;
						case 2:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,true);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,false);
							break;
						case 3:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,true);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,false);
							break;
						case 4:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,true);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,false);
							break;
						case 5:
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_2,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_3,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_4,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_5,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_6,false);
							CCGame->m_uiData->changePlayer(Form_SelWm,B_Form_Block_7,true);
							break;
					}
				}
			}
		}
		for (int i =B_Form_Block_4; i <= B_Form_Block_6;i++ )
		{
			if (CCGame->m_uiData->isPointerInBlock(point.x,point.y,Form_SelPlane,i))
			{
				if (planeIndex != i - B_Form_Block_4)
				{
					planeIndex = i - B_Form_Block_4;
					if (CCGame->isOpenMusic)
						SOUND_PLAY_EFF(SND_OK);
					CCGame->prowuqisound = CCGame->wuqisound;
					CCGame->wuqisound = planeIndex;
					//CCGame->wuqibianhua = true;
					if (CCGame->isOpenMusic)
					{
						switch(CCGame->wuqisound)
						{
						case 0:
							SOUND_PAUSE_EFF();
							SoundPlayer::getInstance()->playEffwuqi1(SND_ZIDAN1);
							break;
						case 1:
							SOUND_PAUSE_EFF();
							SoundPlayer::getInstance()->playEffwuqi2(SND_ZIDAN2);
							break;
						case 2:
							SOUND_PAUSE_EFF();
							SoundPlayer::getInstance()->playEffwuqi3(SND_ZIDAN3);
							break;
						default:
							break;
						}
					}
					CCGame->m_objMgr->curHero->Hero_CG_upDate(planeIndex);
					CC_SAFE_DELETE(plane_player);
					auto_ptr<AnimPlayerFac> facc(new LoopFac());
					plane_player = facc->CreatePlayer(CCGame->m_objMgr->curHero->m_dat[COBJ_DAT_ANIID],CCGame->m_objMgr->curHero->m_dat[COBJ_DAT_ACTION]);
					switch(planeIndex)
					{
					case 0:
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_4,true);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_5,false);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_6,false);
						break;
					case 1:
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_4,false);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_5,true);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_6,false);
						break;
					case 2:
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_4,false);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_5,false);
						CCGame->m_uiData->changePlayer(Form_SelPlane,B_Form_Block_6,true);
						break;
					}

				}
			}
		}
		if (CCGame->m_uiData->isPointerInBlock(point.x,point.y,Form_SelPlane,B_Form_Block_8))
		{
#ifdef CONF_NO_RECORD
			if (CCGame->isOpenMusic)
				SOUND_PLAY_EFF(SND_OK);
			CCGame->hero_animationID = planeIndex+ANI_HERO1_ID;
			CCGame->wingman_ActionID = wingmanIndex;
			DataRecord::getDataRecord();

			if (CCGame->getCurLevelId()==START_LEVEL)
			{
				if (CCGame->isOpenMusic){
					SOUND_STOP_BK();
					SOUND_PLAY_BK(SND_CG);
				}

				SoundPlayer::getInstance()->pauseLoadEff();
				CCGame->SetState(GST_Load/*GST_GameCG*/);
			}
			else
			{
				CCGame->SetState(GST_Load);
			}
#endif
			/*SoundPlayer::getInstance()->playEffqing(SND_QINGPING);
			CocosDenshion::SimpleAudioEngine::sharedEngine()->stopEffect(CCGame->effsoundqing);*/
			SoundPlayer::getInstance()->pauseLoadEff();
			CCGame->SetState(GST_GameSelLevel);
		}
		else if (CCGame->m_uiData->isPointerInBlock(point.x,point.y,Form_SelPlane,B_Form_Block_7))
		{
			if (CCGame->isOpenMusic){
				SOUND_PLAY_EFF(SND_CANCEL);
				SoundPlayer::getInstance()->pauseLoadEff();
			}
			Wingman::getInstance()->Wingman_Set_ActionID(0);
			CCGame->SetState(CCGame->getPreStateFlag());
		}
		//CCGame->m_uiData->touch(point.x,point.y);
	}
}